package org.shop.service.security;

import java.util.ArrayList;
import java.util.Collection;

import org.shop.common.MyConstant;
import org.shop.dao.UsersMapper;
import org.shop.entity.Users;
import org.shop.entity.security.MyUserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService{
	@Autowired
	private UsersMapper usersMapper;
	@Override
	public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
		Users users=usersMapper.findUserByLoginName(userName);
		if(users == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
		if(users.getState()==0){
			throw new UsernameNotFoundException("用户已被禁用");
        }
		MyUserDetail userDetail=null;
		switch (users.getUserType().intValue()) {
		case MyConstant.USER_TYPE_ADMIN:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_ADMIN"));
			break;
		case MyConstant.USER_TYPE_SHOP:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_SHOP"));
			break;
		case MyConstant.USER_TYPE_STUDENT:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_STUDENT"));
			break;
		case MyConstant.USER_TYPE_STUDENTSHOP:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_STUDENTSHOP"));
			break;
		case MyConstant.USER_TYPE_SUPPLIER:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_SUPPLIER"));
			break;
		case MyConstant.USER_TYPE_AUTOTROPHY:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_AUTOTROPHY"));
			break;
		case MyConstant.USER_TYPE_LIVEMANAGER:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_LIVEMANAGER"));
			break;
		case MyConstant.USER_TYPE_BBSMANAGER:
			userDetail=new MyUserDetail(users.getLoginName(), users.getPassword(), getAuthorities("ROLE_BBSMANAGER"));
			break;
		}
		userDetail.setUserName(users.getUserName());
		userDetail.setLoginName(users.getLoginName());
		userDetail.setUserId(users.getUserId());
		userDetail.setPassword(users.getPassword());
		userDetail.setRealName(users.getRealName());
		userDetail.setUserType(users.getUserType());
		userDetail.setIdNo(users.getIdNo());
		userDetail.setAddressId(users.getAddressId());
		userDetail.setEmail(users.getEmail());
		userDetail.setPhone(users.getPhone());
		userDetail.setConstellation(users.getConstellation());
		userDetail.setUserPhoto(users.getUserPhoto());
		userDetail.setSchoolId(users.getSchoolId());
		userDetail.setMerchantId(users.getMerchantId());
		userDetail.setState(users.getState());
		return userDetail;
	}
	private Collection<GrantedAuthority> getAuthorities(String role) {
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(role);
        grantedAuthorities.add(grantedAuthority);
        return grantedAuthorities;
    }
	
}
